package xionglizhen.service;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xionglizhen.entity.Illness;
import xionglizhen.entity.IllnessKind;
import xionglizhen.entity.PageView;
import xionglizhen.mapper.IllnessKindMapper;
import xionglizhen.mapper.IllnessMapper;
import xionglizhen.mapper.PageViewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xionglizhen.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IllnessService extends ServiceImpl<IllnessMapper, Illness> {
    @Autowired
    private IllnessKindMapper illnessKindMapper;
    @Autowired
    private PageViewMapper pageViewMapper;
    @Autowired
    private IllnessMapper illnessMapper;

    public IPage<Illness> getIllnessPageDataByIllnessName(String illnessName, int pageSize, int curPage) {
        Page<Illness> IllnessPage = new Page<>(curPage, pageSize);
        QueryWrapper<Illness> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(illnessName != null, "illness_name", illnessName);
        IPage<Illness> IllnessIPage = this.page(IllnessPage,queryWrapper);
        List<Illness> records = IllnessIPage.getRecords();
        if (records.size() > 0) {
            records.forEach(s -> {
                s.setKindName(illnessKindMapper.selectById(s.getKindId()).getName());
                QueryWrapper<PageView> wrapper = new QueryWrapper<>();
                wrapper.eq("illness_id", s.getId());
                s.setPageView(pageViewMapper.selectOne(wrapper).getPageViews());
            });
        }
        return IllnessIPage;
    }



}
